package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.PayInfoBean;
import com.example.administrator.familfinalcialmanagement.bean.WalletBean;
import com.example.administrator.familfinalcialmanagement.greendao.gen.PayInfoBeanDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.WalletBeanDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.dialog.MyDialog;
import com.example.administrator.familfinalcialmanagement.util.listener.OnGRClickListener;

import java.util.ArrayList;
import java.util.List;

public class MyMoneyActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout lin_xianjin, lin_zhifubao, lin_yinhangka, lin_xinyongka, lin_weixin;
    private TextView tv_xianjin, tv_zhifubao, tv_yinhangka, tv_xinyongka, tv_weixin, tv_zongzican, tv_jingzican, tv_zhichu, tv_shouru;
    private MyDialog dialog;
    private int tag = 0;
    WalletBeanDao walletBeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getWalletBeanDao();
    List<WalletBean> list = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_money);
        initView();
    }


    private void initView() {
        setBack();
        setTitleText("钱包");
        lin_xianjin = findViewById(R.id.lin_xianjin);
        lin_zhifubao = findViewById(R.id.lin_zhifubao);
        lin_yinhangka = findViewById(R.id.lin_yinhangka);
        lin_xinyongka = findViewById(R.id.lin_xinyongka);
        lin_weixin = findViewById(R.id.lin_weixin);
        tv_xianjin = findViewById(R.id.tv_xianjin);
        tv_zhifubao = findViewById(R.id.tv_zhifubao);
        tv_yinhangka = findViewById(R.id.tv_yinhangka);
        tv_xinyongka = findViewById(R.id.tv_xinyongka);
        tv_weixin = findViewById(R.id.tv_weixin);
        tv_zongzican = findViewById(R.id.tv_zongzican);
        tv_jingzican = findViewById(R.id.tv_jingzican);
        tv_zhichu = findViewById(R.id.tv_zhichu);
        tv_shouru = findViewById(R.id.tv_shouru);


        //查找全部钱包
        refresh();
        if (list == null || list.size() < 2) {//没有就初始化钱包
            WalletBean bean = new WalletBean();
            bean.setMoney("0");
            bean.setWalletName("现金");
            bean.setWalletStyle("1");
            //插入钱包实体类
            walletBeanDao.insert(bean);

            WalletBean bean2 = new WalletBean();
            bean2.setMoney("0");
            bean2.setWalletName("信用卡");
            bean2.setWalletStyle("2");
            //插入钱包实体类
            walletBeanDao.insert(bean2);

            WalletBean bean3 = new WalletBean();
            bean3.setMoney("0");
            bean3.setWalletName("支付宝");
            bean3.setWalletStyle("3");
            //插入钱包实体类
            walletBeanDao.insert(bean3);

            WalletBean bean4 = new WalletBean();
            bean4.setMoney("0");
            bean4.setWalletName("微信钱包");
            bean4.setWalletStyle("4");
            //插入钱包实体类
            walletBeanDao.insert(bean4);

            WalletBean bean5 = new WalletBean();
            bean5.setMoney("0");
            bean5.setWalletName("储蓄卡");
            bean5.setWalletStyle("5");
            //插入钱包实体类
            walletBeanDao.insert(bean5);
            refresh();
        }
    }

    //刷新数据
    public void refresh() {
        list = walletBeanDao.loadAll();
        if (list == null || list.size() < 2) {
            return;
        }
        try {
            tv_xianjin.setText(list.get(0).getMoney());
            tv_xinyongka.setText(list.get(1).getMoney());
            tv_zhifubao.setText(list.get(2).getMoney());
            tv_weixin.setText(list.get(3).getMoney());
            tv_yinhangka.setText(list.get(4).getMoney());
        } catch (Exception e) {
            Toast.makeText(MyMoneyActivity.this, "数据查找异常", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
        try {
            //统计支出
            int number = 0;
            PayInfoBeanDao daobeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getPayInfoBeanDao();
            List<PayInfoBean> infoBeans = daobeanDao.queryBuilder().where(PayInfoBeanDao.Properties.PayStatus.eq("支出"), PayInfoBeanDao.Properties.PayStatus.eq("支出")).list();
            for (int i = 0; i < infoBeans.size(); i++) {
                number = number + Integer.getInteger(infoBeans.get(i).getMoney());
            }
            tv_zhichu.setText("支出：" + number);
            //统计收入
            int numbesr = 0;

            List<PayInfoBean> infoBeans2 = daobeanDao.queryBuilder().where(PayInfoBeanDao.Properties.PayStatus.eq("收入"), PayInfoBeanDao.Properties.PayStatus.eq("收入")).list();
            for (int i = 0; i < infoBeans2.size(); i++) {
                numbesr = numbesr + Integer.getInteger(infoBeans2.get(i).getMoney());
            }
            tv_shouru.setText("收入：" + numbesr);

            //算净资产
            int jingzican = 0;
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    jingzican = jingzican + Integer.valueOf(list.get(i).getMoney());
                }
            }
            tv_jingzican.setText("净资产：" + jingzican);
            //算总余额
            tv_zongzican.setText("总资产：" + jingzican + "");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.lin_xianjin://现金
                tag = 1;
                showDialog();
                break;
            case R.id.lin_xinyongka://信用卡
                tag = 2;
                showDialog();
                break;
            case R.id.lin_zhifubao://支付宝
                tag = 3;
                showDialog();
                break;
            case R.id.lin_weixin://微信
                tag = 4;
                showDialog();
                break;
            case R.id.lin_yinhangka://银行卡
                tag = 5;
                showDialog();
                break;
        }
        super.onClick(view);
    }

    //显示弹框
    public void showDialog() {

        dialog = new MyDialog(MyMoneyActivity.this);
        dialog.show();
        dialog.setOnGRClickListener(new OnGRClickListener() {
            @Override
            public void setOnConfirmClick(String res) {
                ToastUtil.showToast("输入的金额：" + res);
                setDB(res);
            }

            @Override
            public void setOnCloseClick() {
                dialog.dismiss();
            }
        });

    }

    //存数据库
    public void setDB(String money) {
        //tag代表不同的账户类型
        WalletBean bean = list.get(tag - 1);
        bean.setMoney(money);
        walletBeanDao.update(bean);
        refresh();

    }

    //查询数据库
    public void getDB() {


        //设置值
        tv_zongzican.setText("");//总资产
        tv_jingzican.setText("");//净资产
        tv_zhichu.setText("");//支出
        tv_shouru.setText("");//收入
        tv_xianjin.setText("");//现金
        tv_xinyongka.setText("");//信用卡
        tv_zhifubao.setText("");//支付宝
        tv_weixin.setText("");//微信钱包
        tv_yinhangka.setText("");//银行卡


    }
}